﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for ProductDAL
/// </summary>
public class ProductDAL
{
    DatabaseAccess dba = new DatabaseAccess();

    public ProductDAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public DataTable GetAllProduct()
    {
        return dba.executeSelect("proc_GetProduct");
    }

    public DataTable GetProductByID(int id)
    {
        SqlParameter[] param = new SqlParameter[1];
        param[0] = new SqlParameter("@pd_id", SqlDbType.Int);
        param[0].Value = id;
        return dba.executeSelect("proc_GetProductByID",param);
    }

    public DataTable GetProductByCategory(int cat_id)
    {
        SqlParameter[] paramList = new SqlParameter[1];
        paramList[0] = new SqlParameter("@cat_id",SqlDbType.Int);
        paramList[0].Value = cat_id;
        return dba.executeSelect("proc_getProductByCategoryID",paramList);
    }
    public DataTable searchProductByName(String pd_name)
    {
        SqlParameter[] paramList = new SqlParameter[1];
        paramList[0] = new SqlParameter("@pd_name",SqlDbType.NVarChar,50);
        paramList[0].Value = pd_name;
        return dba.executeSelect("proc_searchProductByName",paramList);
    }
    public DataTable searchProductByPrice(decimal pd_price)
    {
        SqlParameter[] paramList = new SqlParameter[1];      
        paramList[0] = new SqlParameter("@pd_price",SqlDbType.Money);
        paramList[0].Value = pd_price;
        return dba.executeSelect("proc_GetProductByPrice",paramList);
    }
    public bool InsertProduct(Product objProduct)
    {
        SqlParameter[] paramList = new SqlParameter[6];
        paramList[0] = new SqlParameter("@cat_id",SqlDbType.Int);
        paramList[0].Value = objProduct.CatID;
        paramList[1] = new SqlParameter("@pd_name",SqlDbType.NVarChar,50);
        paramList[1].Value = objProduct.PdName;
        paramList[2] = new SqlParameter("@pd_description",SqlDbType.NVarChar,200);
        paramList[2].Value = objProduct.PdDescription;
        paramList[3] = new SqlParameter("@pd_quantity",SqlDbType.Int);
        paramList[3].Value = objProduct.PdQuantity;
        paramList[4] = new SqlParameter("@pd_price",SqlDbType.Money);
        paramList[4].Value = objProduct.PdPrice;
        paramList[5] = new SqlParameter("@pd_image",SqlDbType.NVarChar,200);
        paramList[5].Value = objProduct.PdImage;
        return dba.executeUpdate("proc_insertProduct", paramList);
    }
    public bool UpdateProduct(Product objProduct)
    {
        SqlParameter[] paramList = new SqlParameter[7];
        paramList[0] = new SqlParameter("@cat_id", SqlDbType.Int);
        paramList[0].Value = objProduct.CatID;
        paramList[1] = new SqlParameter("@pd_name", SqlDbType.NVarChar, 50);
        paramList[1].Value = objProduct.PdName;
        paramList[2] = new SqlParameter("@pd_description", SqlDbType.NVarChar, 200);
        paramList[2].Value = objProduct.PdDescription;
        paramList[3] = new SqlParameter("@pd_quantity", SqlDbType.Int);
        paramList[3].Value = objProduct.PdQuantity;
        paramList[4] = new SqlParameter("@pd_price", SqlDbType.Money);
        paramList[4].Value = objProduct.PdPrice;
        paramList[5] = new SqlParameter("@pd_image", SqlDbType.NVarChar, 200);
        paramList[5].Value = objProduct.PdImage;
        paramList[6] = new SqlParameter("@pd_id",SqlDbType.Int);
        paramList[6].Value = objProduct.PdID;
        return dba.executeUpdate("proc_UpdateProduct", paramList);
    }
    public bool DeleteProduct(int pdID)
    {
        SqlParameter[] paramList = new SqlParameter[1];
        paramList[0] = new SqlParameter("@pd_id",SqlDbType.Int);
        paramList[0].Value = pdID;
        return dba.executeUpdate("proc_deleteProduct",paramList);
    }
    public bool UpdateVisitProduct(int pd_id,int pd_visit)
    {
        SqlParameter[] paramList = new SqlParameter[2];
        paramList[0] = new SqlParameter("@pd_id",SqlDbType.Int);
        paramList[0].Value = pd_id;
        paramList[1] = new SqlParameter("@pd_visit",SqlDbType.Int);
        paramList[1].Value = pd_visit;
        return dba.executeUpdate("proc_UpdateVisitProduct",paramList);
    }
    public DataTable GetVisitProduct(int pd_id)
    {
        SqlParameter[] paramList = new SqlParameter[1];
        paramList[0] = new SqlParameter("@pd_id",SqlDbType.Int);
        paramList[0].Value = pd_id;
        return dba.executeSelect("proc_GetVisitProduct",paramList);
    }
    public bool UpdateOrderNum(int pd_id, int pd_ordernum)
    {
        SqlParameter[] paramList = new SqlParameter[2];
        paramList[0] = new SqlParameter("@pd_id", SqlDbType.Int);
        paramList[0].Value = pd_id;
        paramList[1] = new SqlParameter("@pd_ordernum", SqlDbType.Int);
        paramList[1].Value = pd_ordernum;
        return dba.executeUpdate("proc_UpdateOrderNum", paramList);
    }
    public DataTable GetOrderNum(int pd_ordernum)
    {
        SqlParameter[] paramList = new SqlParameter[1];
        paramList[0] = new SqlParameter("@pd_id", SqlDbType.Int);
        paramList[0].Value = pd_ordernum;
        return dba.executeSelect("proc_GetCurrentOrderNum", paramList);
    }
}
